<?php

/*
 * Yanis Framework, Yet ANother Internet Service for web and mobile technologists
 * @copyright Copyright (C) 2011 ch@rlesen. All rights reserved.
 * @license GNU/GPL
 * @author Charles EDOU NZE <charles at charlesen.fr>
 */

/**
 * Class url
 */
class Url {

    /**
     * @var Object The Registry object 
     */
    private $registry;

    /**
     * @var String The URL Path
     */
    private $_urlpath;

    /**
     * @var Array Url bits 
     */
    private $_urlBits;

    /*
     * The getURLData method processes the incoming URL, and breaks it down into
     * parts, building up an array of "URL bits"
     * 
     * Gets data from the current URL
     */

    public function __construct(Registry $registry) {
        $this->registry = $registry;
    }

    public function getURLData() {
        $param = 'page';
        $urldata = (isset($_GET[$param])) ? $_GET[$param] : '';
        $this->_urlpath = $urldata;
        if ($urldata == '') {
            $this->_urlBits[] = '';
            $this->_urlpath = '';
        } else {
            $data = explode('/', $urldata);
            while (!empty($data) && strlen(reset($data)) === 0) {
                array_shift($data);
            }
            while (!empty($data) && strlen(end($data)) === 0) {
                array_pop($data);
            }
            $this->_urlBits = trim($data);
        }
    }

    /**
     * @return Array array of bits 
     */
    public function getURLBits() {
        return $this->_urlBits;
    }

    /**
     * Access to a specific bit 
     */
    public function getURLBits($thisBit) {
        return (isset($this->_urlBits[$thisBit])) ? $this->_urlBits[$thisBit] : 0;
    }

    /**
     * Set the URL Path
     * @param String $path the url path
     */
    public function setURLPath($path) {
        $this->_urlpath = $path;
    }

    /**
     * Get the path URL
     * @return type 
     */
    public function getURLPath() {
        return $this->_urlpath;
    }

    public function renderURL($bits, $qs, $admin) {
        $admin = ($admin == 1) ? $this->registry->getSetting('admin_folder') . '/' : '';
        $the_rest = '';
        foreach ($bits as $bit) {
            $the_rest .= $bit . '/';
        }
        $the_rest = ($qs != '') ? $the_rest . '?&' . $qs : $the_rest;
        return $this->registry->getSetting('siteurl') . $admin . $the_rest;
    }

}

?>